################################################################################
##                      Replication file for:                                 ##
##                                                                            ##
##          The Institutional Sources of Economic Transformation:             ##
##    Insulation and Compensation in the Politics of Energy Transitions       ##
##                                                                            ##
##     Jared J. Finnegan, Phillip Lipscy, Jonas Meckling, Florence Metz       ##
################################################################################

install.packages('gsynth', type = 'source')
install.packages('panelView')
install.packages("readstata13")
install.packages("GGIR",dependencies = TRUE)
install.packages('devtools', repos = 'http://cran.us.r-project.org') 

library(gsynth)
library(haven)
library(panelView)
library(readstata13)

################################################################################
############################ Main results ######################################
################################################################################


###### Demand-Side Analysis  ######

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1965_1985.dta")
oilshocks$shockdemand <- oilshocks$d_side_dummy * oilshocks$shock73

system.time(
  out <- gsynth(gas_tax_nomusd_marketxr ~ shockdemand + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc + vehicle_pc + urban_pop , data = oilshocks, 
                index = c("countryid","year"), force = "two-way",
                CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                inference = "parametric", nboots = 1000,
                parallel = TRUE, na.rm=TRUE)
)

# Figure 1: The Effect of Demand-Side Institutions on Gasoline Taxes (Generalized Synthetic Control Method) (left panel)
plot(out, type = "counterfactual", raw = "none", main="", ylab="Gasoline Tax (USD per liter)", theme.bw = TRUE)

# Figure 1: The Effect of Demand-Side Institutions on Gasoline Taxes (Generalized Synthetic Control Method) (right panel)
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)

#Figure A3. Raw data for supply-side analysis
plot(out, type = "raw", id = "country")

#Table A4. Estimates from generalized synthetic control method (Model 2)
print(out)


###### Supply-Side Analysis  ######

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shocksupply <- oilshocks$s_side_dummy * oilshocks$shock73

system.time(
    out <- gsynth(Electricityproductionfromoil ~ shocksupply + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

#Figure 2. The Effect of Supply-Side Institutions on Oil Share of Electricity Production (Generalized Synthetic Control Method) (Left panel)
plot(out, type = "counterfactual", raw = "none", main="", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

#Figure 2. The Effect of Supply-Side Institutions on Oil Share of Electricity Production (Generalized Synthetic Control Method) (Right panel)
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)

#Figure A3. Raw data for supply-side analysis
plot(out, type = "raw", id = "country")

#Table A4. Estimates from generalized synthetic control method (Model 1)
print(out)


################################################################################
############### Supplementary Information ######################################
################################################################################

##########  Figure A5. Generalized synthetic control results with only key institutional variable ##########  

## Supply-Side Analysis 

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shocksupply <- oilshocks$s_side_dummy * oilshocks$shock73

system.time(
    out <- gsynth(Electricityproductionfromoil ~ shocksupply , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
 )

# Top left panel
plot(out, type = "counterfactual", raw = "none", main="Supply-side analysis", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

# Top right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


## Demand-Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1965_1985.dta")
oilshocks$shockdemand <- oilshocks$d_side_dummy * oilshocks$shock73

system.time(
    out <- gsynth(gas_tax_nomusd_marketxr ~ shockdemand , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

# Bottom left panel
plot(out, type = "counterfactual", raw = "none", main="Demand-side analysis", ylab="Gasoline Tax (USD per liter)", theme.bw = TRUE)

# Bottom right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


########## Figure A6. Generalized synthetic control results with additional covariates ##########  

## Supply Side Analysis 

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shocksupply <- oilshocks$s_side_dummy * oilshocks$shock73

system.time(
    out <- gsynth(Electricityproductionfromoil ~ shocksupply + singleparty + polconiii + gov_left1 + openc + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc, data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

# Top left panel
plot(out, type = "counterfactual", raw = "none", main="Supply-side analysis", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

# Top right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


## Demand Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1965_1985.dta")
oilshocks$shockdemand <- oilshocks$d_side_dummy * oilshocks$shock73

system.time(
    out <- gsynth(gas_tax_nomusd_marketxr ~ shockdemand + singleparty + polconiii + gov_left1 + openc + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc + vehicle_pc + urban_pop , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

# Bottom left panel
plot(out, type = "counterfactual", raw = "none", main="Demand-side analysis", ylab="Gasoline Tax (USD per liter)", theme.bw = TRUE)

# Bottom right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


########## Figure A7. Generalized synthetic control results with pretreatment period 1965-1972 ########## 

## Supply Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1965_1985.dta")
oilshocks$shocksupply <- oilshocks$s_side_dummy * oilshocks$shock73

system.time(
    out <- gsynth(Electricityproductionfromoil ~ shocksupply + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

# Left panel
plot(out, type = "counterfactual", raw = "none", main="Supply-side analysis", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

# Right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


########## Figure A8. Generalized synthetic control placebo treatment test 1 ########## 

## Supply Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shockdemand <- oilshocks$d_side_dummy * oilshocks$shock73

system.time(
  out <- gsynth(Electricityproductionfromoil ~ shockdemand + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc , data = oilshocks, 
                index = c("countryid","year"), force = "two-way",
                CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                inference = "parametric", nboots = 1000,
                parallel = TRUE, na.rm=TRUE)
)

# Top left panel
plot(out, type = "counterfactual", raw = "none", main="Placebo treatment test (Supply-side)", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

# Top right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


## Demand Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1965_1985.dta")
oilshocks$shocksupply <- oilshocks$s_side_dummy * oilshocks$shock73

system.time(
  out <- gsynth(gas_tax_nomusd_marketxr ~ shocksupply + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc + vehicle_pc + urban_pop , data = oilshocks, 
                index = c("countryid","year"), force = "two-way",
                CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                inference = "parametric", nboots = 1000,
                parallel = TRUE, na.rm=TRUE)
)

# Bottom left panel
plot(out, type = "counterfactual", raw = "none", main="Placebo treatment test (Demand side)", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

# Bottom right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


########## Figure A9. Generalized synthetic control placebo treatment test 2 ##########

##Supply Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shock68 <- ifelse(oilshocks$year > 1967, 1, 0)
oilshocks$shocksupply68 <- oilshocks$s_side_dummy * oilshocks$shock68

system.time(
  out <- gsynth(Electricityproductionfromoil ~ shocksupply68 + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc , data = oilshocks, 
                index = c("countryid","year"), force = "two-way",
                CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                inference = "parametric", nboots = 1000,
                parallel = TRUE, na.rm=TRUE, min.T0 = 6)
)

# Left panel
plot(out, type = "counterfactual", raw = "none", main="Placebo treatment test (Supply side)", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

# Right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since placebo oil price shock", theme.bw = TRUE)


########## Figure A10. Generalized synthetic control placebo outcome tests ########## 

##Supply Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shocksupply <- oilshocks$s_side_dummy * oilshocks$shock73

system.time(
  out <- gsynth(elect_gen_pc ~ shocksupply + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc, data = oilshocks, 
                index = c("countryid","year"), force = "two-way",
                CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                inference = "parametric", nboots = 1000,
                parallel = TRUE, na.rm=TRUE)
)

# Top left panel
plot(out, type = "counterfactual", raw = "none", main="Placebo outcome test (Supply side)", ylab="Electricity generation per capita (TWh per million people)", theme.bw = TRUE)

# Top right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


##Demand Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1965_1985.dta")
oilshocks$shockdemand <- oilshocks$d_side_dummy * oilshocks$shock73

system.time(
    out <- gsynth(vehicle_pc ~ shockdemand + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc + urban_pop , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

# Bottom left panel
plot(out, type = "counterfactual", raw = "none", main="Placebo outcome test (Demand side)", ylab="Vehicle ownership (vehicles per 1,000 people)", theme.bw = TRUE)

#Bottom right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


########## Figure A11. Generalized synthetic control – Flipped treatment ########## 

## Supply Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shocksupply <- oilshocks$s_side_dummy_flip * oilshocks$shock73

system.time(
    out <- gsynth(Electricityproductionfromoil ~ shocksupply + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

# Top right panel
plot(out, type = "counterfactual", raw = "none", main="Supply-side analysis", ylab="Oil Share of Electricity Production (%)", theme.bw = TRUE)

# Top right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


## Demand Side Analysis

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1965_1985.dta")
oilshocks$shockdemand <- oilshocks$d_side_dummy_flip * oilshocks$shock73

system.time(
    out <- gsynth(gas_tax_nomusd_marketxr ~ shockdemand + prod_oil_pc + realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc + vehicle_pc + urban_pop , data = oilshocks, 
                  index = c("countryid","year"), force = "two-way",
                  CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                  inference = "parametric", nboots = 1000,
                  parallel = TRUE, na.rm=TRUE)
)

# Bottom right panel
plot(out, type = "counterfactual", raw = "none", main="Demand-side analysis", ylab="Gasoline Tax (USD per liter)", theme.bw = TRUE)

# Bottom left panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)


########## Figure A13. Institutions and total oil consumption ########## 

## The "shockdemand" dummy can be used for this because all countries that have either insulating or compensatory institutions on the supply or demand side are coded as one (see Table A2)

oilshocks <- read.dta13(INSERT DIRECTORY TO FILE "Finnegan et al_The Institutional Sources of Economic Transformation_1960_1985.dta")
oilshocks$shockdemand <- oilshocks$d_side_dummy * oilshocks$shock73

system.time(
  out <- gsynth(tpes_oil_percent ~ shockdemand + prod_oil_pc +  realgdpgr + inflation + unemp + GDPPPPbillion2010USdollars_pc, data = oilshocks, 
                index = c("countryid","year"), force = "two-way",
                CV = FALSE, r = c(0, 5), se = TRUE, EM = TRUE,
                inference = "parametric", nboots = 1000,
                parallel = TRUE, na.rm=TRUE)
)

# Left panel
plot(out, type = "counterfactual", raw = "none", main="", ylab="Oil Share of TPES (%)", theme.bw = TRUE)

# Right panel
plot(out, main="Average Treatment Effect on the Treated (ATT)", ylab="Estimated Effect", xlab="Years since oil price shock", theme.bw = TRUE)

